//
//  TCLYTaskViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/8/20.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYTaskViewController.h"
#import "TCLYTaskTableViewCell.h"
#import "AppDelegate.h"
#import "WXApi.h"
#import "TCLYMineModel.h"
#import "Masonry.h"
#import "Macros.h"
#import "TCLYSigninViewController.h"
#import "TCLYTopUpViewController.h"
#import "shaiDanViewController.h"
#import "xinShengErHuLiViewController.h"
#import "changeNinaPagerViewHeight.h"

@interface TCLYTaskViewController ()<UITableViewDataSource, UITableViewDelegate,WXDelegate>{
        AppDelegate *appdelegate;
}

@property (nonatomic, strong) UITableView * taskTabelView;
@property (nonatomic, strong) TCLYMineModel * mineModel;

@end

@implementation TCLYTaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"任务";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    // Do any additional setup after loading the view.
    self.mineModel = [[TCLYMineModel alloc] init];
    [self.mineModel loadCellLabelText];
    [self createView];
}
-(void) createView{
    self.taskTabelView = [[UITableView alloc] init];
    self.taskTabelView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.taskTabelView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.taskTabelView.delegate = self;
    self.taskTabelView.dataSource = self;
    [self.view addSubview:self.taskTabelView];
    [self.taskTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}
//tabelView必须实现的两个代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mineModel.taskNameArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"taskCell";
    TCLYTaskTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TCLYTaskTableViewCell alloc] initWithReuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.taskNameLabel.text = self.mineModel.taskNameArr[indexPath.row];
    CGSize taskNameSize = [cell.taskNameLabel sizeThatFits:CGSizeMake(cell.taskNameLabel.bounds.size.width, MAXFLOAT)];
    [cell.taskNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cell.rewardLabel);
        make.left.mas_equalTo(cell.mas_left).offset(12);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(taskNameSize.width);
    }];
    cell.rewardLabel.text = self.mineModel.rewardArr[indexPath.row];
    
    [cell.taskBtn setTitle:self.mineModel.taskArr[indexPath.row] forState:UIControlStateNormal];
    
    return cell;
}
//点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            TCLYSigninViewController * signinViewCtr = [[TCLYSigninViewController alloc] init];
            signinViewCtr.isMineViewCtr = NO;
            self.navigationController.navigationBarHidden = NO;
            [self.navigationController pushViewController:signinViewCtr animated:YES];
        }
            break;
        case 1:{
            //这个是分享给好友
            [self isShareToPengyouquan:NO];
        }
            break;
        case 2:{
            changeNinaPagerViewHeight *cnpv=[changeNinaPagerViewHeight shardeManager];
            cnpv.NinaPagerViewHeight=150;
            xinShengErHuLiViewController *xsehlvc=[xinShengErHuLiViewController new];
            [self.navigationController pushViewController:xsehlvc animated:NO];
        }
            break;
        case 3:{
            changeNinaPagerViewHeight *cnpv=[changeNinaPagerViewHeight shardeManager];
            cnpv.NinaPagerViewHeight=150;
            xinShengErHuLiViewController *xsehlvc=[xinShengErHuLiViewController new];
            [self.navigationController pushViewController:xsehlvc animated:NO];
        }
            break;
        case 4:{
            //这个是分享到朋友圈
            [self isShareToPengyouquan:YES];
            
        }
            break;
        case 5:{
            shaiDanViewController *sdvc=[shaiDanViewController new];
            [self.navigationController pushViewController:sdvc animated:NO];
        }
            break;
        case 6:{
            //任务充值
            TCLYTopUpViewController * topUpViewCtr = [[TCLYTopUpViewController alloc] init];
            [self.navigationController pushViewController:topUpViewCtr animated:YES];
        }
            break;
    }
}
//每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *) sender).tag) {
        case 91:{
            self.navigationController.navigationBarHidden = YES;
            self.tabBarController.tabBar.hidden = NO;
            [self.navigationController popViewControllerAnimated:YES];
            
        }
            break;
    }
}


#pragma mark 微信好友分享
/**
 *  微信分享对象说明
 *
 *  @param sender
 WXMediaMessage    多媒体内容分享
 WXImageObject      多媒体消息中包含的图片数据对象
 WXMusicObject      多媒体消息中包含的音乐数据对象
 WXVideoObject      多媒体消息中包含的视频数据对象
 WXWebpageObject    多媒体消息中包含的网页数据对象
 WXAppExtendObject  返回一个WXAppExtendObject对象
 WXEmoticonObject   多媒体消息中包含的表情数据对象
 WXFileObject       多媒体消息中包含的文件数据对象
 WXLocationObject   多媒体消息中包含的地理位置数据对象
 WXTextObject       多媒体消息中包含的文本数据对象
 
 */


-(void)isShareToPengyouquan:(BOOL)isPengyouquan{
    /** 标题
     * @note 长度不能超过512字节
     */
    // @property (nonatomic, retain) NSString *title;
    /** 描述内容
     * @note 长度不能超过1K
     */
    //@property (nonatomic, retain) NSString *description;
    /** 缩略图数据
     * @note 大小不能超过32K
     */
    //  @property (nonatomic, retain) NSData   *thumbData;
    /**
     * @note 长度不能超过64字节
     */
    // @property (nonatomic, retain) NSString *mediaTagName;
    /**
     * 多媒体数据对象，可以为WXImageObject，WXMusicObject，WXVideoObject，WXWebpageObject等。
     */
    // @property (nonatomic, retain) id        mediaObject;
    
    /*! @brief 设置消息缩略图的方法
     *
     * @param image 缩略图
     * @note 大小不能超过32K
     */
    //- (void) setThumbImage:(UIImage *)image;
    //缩略图
    UIImage *image = [UIImage imageNamed:@"bgHeader"];
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = @"微信分享测试";
    message.description = @"微信分享测试----描述信息";
    //png图片压缩成data的方法，如果是jpg就要用 UIImageJPEGRepresentation
    message.thumbData = UIImagePNGRepresentation(image);
    [message setThumbImage:image];
    
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = @"http://www.baidu.com";
    message.mediaObject = ext;
    message.mediaTagName = @"ISOFTEN_TAG_JUMP_SHOWRANK";
    
    SendMessageToWXReq *sentMsg = [[SendMessageToWXReq alloc]init];
    sentMsg.message = message;
    sentMsg.bText = NO;
    //选择发送到会话(WXSceneSession)或者朋友圈(WXSceneTimeline)
    if (isPengyouquan) {
        sentMsg.scene = WXSceneTimeline;  //分享到朋友圈
    }else{
        sentMsg.scene =  WXSceneSession;  //分享到会话。
    }
    
    //如果我们想要监听是否成功分享，我们就要去appdelegate里面 找到他的回调方法
    // -(void) onResp:(BaseResp*)resp .我们可以自定义一个代理方法，然后把分享的结果返回回来。
    appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.wxDelegate = self;
//    BOOL isSuccess = [WXApi sendReq:sentMsg];
    
    
    //添加对appdelgate的微信分享的代理
    
}

#pragma mark 监听微信分享是否成功 delegate
-(void)shareSuccessByCode:(int)code{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享成功" message:[NSString stringWithFormat:@"reason : %d",code] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
